package com.diandiansong.app.widgets;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diandiansong.app.R;

/**
 * Created by chris on 2018/3/22.
 * 加减组合控件
 */

public class AddAndMinusViewInItem extends LinearLayout {


    private TextView et_num;
    View tv_add;
    View tv_minus;
    int max = Integer.MAX_VALUE;
    int min = 1;

    public AddAndMinusViewInItem(Context context) {
        super(context);
        init(context);
    }

    public AddAndMinusViewInItem(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AddAndMinusViewInItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_add_minus_in_item, this);
        tv_minus = findViewById(R.id.tv_minus);
        et_num = findViewById(R.id.et_num);
        tv_add = findViewById(R.id.tv_add);
        tv_minus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.parseInt(et_num.getText().toString());
                num--;
                if (num >= min && num <= max) {
                    setNum(num);

                    if (mOnChangeListener != null) {
                        mOnChangeListener.onChange(num);
                    }
                }
            }
        });

        tv_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAddListener != null) {
                    mAddListener.onClick(v);
                    return;
                }

                int num = Integer.parseInt(et_num.getText().toString());
                num++;

                if (num >= min && num <= max) {
                    setNum(num);

                    if (mOnChangeListener != null) {
                        mOnChangeListener.onChange(num);
                    }
                }

            }
        });
    }

    //设置数字
    public void setNum(int num) {
        et_num.setText(num + "");

        if (num == max) {
            et_num.setVisibility(VISIBLE);
            tv_minus.setVisibility(VISIBLE);
            tv_add.setVisibility(INVISIBLE);

        } else if (num > min && num < max) {
            et_num.setVisibility(VISIBLE);
            tv_minus.setVisibility(VISIBLE);
            tv_add.setVisibility(VISIBLE);

        } else if (num == min) {
            et_num.setVisibility(VISIBLE);
            tv_minus.setVisibility(VISIBLE);
            tv_add.setVisibility(VISIBLE);
        }
    }

    public void setMax(int num) {
        this.max = num;
        if (getNum() > max) {
            setNum(max);

        } else {//刷新
            setNum(getNum());
        }
    }

    public void setMin(int num) {
        this.min = num;
        if (getNum() < num) {
            setNum(min);

        } else {//刷新
            setNum(getNum());
        }
    }

    //获取数字
    public int getNum() {
        return Integer.parseInt(et_num.getText().toString());
    }

    public interface OnChangeListener {
        void onChange(int num);

    }

    public void setOnChangeListener(OnChangeListener onChangeListener) {
        mOnChangeListener = onChangeListener;
    }

    OnChangeListener mOnChangeListener;

    View.OnClickListener mAddListener;

    public void setAddListener(View.OnClickListener onClickListener) {
        this.mAddListener = onClickListener;
    }
}
